package com.gxa.personal.controller;

import com.gxa.common.base.RespResult;
import com.gxa.personal.entity.UserInformation;
import com.gxa.personal.service.UserInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletResponse;
import java.io.*;
import java.util.Base64;
import java.util.Locale;
import java.util.UUID;

/**
 * Create with IntelliJ IDEA.
 * Description:
 * User: ZY
 * Date: 2023-04-10
 * Time: 18:45
 */
@Controller
@RequestMapping("/information")
public class UserInformationController {

    @Autowired
    UserInformationService userInformationService;

    @PostMapping("/modify")
    @ResponseBody
    public RespResult modify(@RequestBody UserInformation userInformation) {

        return userInformationService.modify(userInformation);
    }

    /**
     * base64 图片上传
     * @param base64
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public RespResult upload(@RequestParam("base64")String base64){
        String base64Data = base64.split(",")[1];
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] bytes = decoder.decode(base64Data);
        // 获取当前操作系统
        String osName = System.getProperties().get("os.name").toString().toLowerCase(Locale.ROOT);
        String path="";

        if(osName.startsWith("win")){
            path="F:\\Test\\";
        }else{
            path="/mnt/test";
        }
        String newName= UUID.randomUUID().toString()+".png";
        File saveFile=new File(path+newName);
        if(!saveFile.getParentFile().exists()){
            saveFile.getParentFile().mkdirs();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(saveFile);
            fos.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return RespResult.success(saveFile.getPath());
    }



    @GetMapping("/findMy")
    @ResponseBody
    public RespResult findMy(Integer userInformationId) {
        return userInformationService.findOne(userInformationId);
    }

    @PostMapping("/downloadImg")
    public void downloadImg(String path, ServletResponse response) throws IOException {

        File filePic = new File(path+"1-ab1.png");
        if(filePic.exists()){
            FileInputStream is = new FileInputStream(filePic);
            int i = is.available(); // 得到文件大小
            byte data[] = new byte[i];
            is.read(data); // 读数据
            is.close();
            response.setContentType("image/*"); // 设置返回的文件类型
            OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
            toClient.write(data); // 输出数据
            toClient.close();
        }
    }


    @GetMapping("/updatePhone")
    @ResponseBody
    public RespResult updatePhone(@RequestBody UserInformation userInformation){
        return userInformationService.updatePhone(userInformation);
    }
}
